package com.hyg.lib_music.ui.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.media.AudioManager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.hyg.lib_music.R;
import com.hyg.lib_music.service.MusicService;
import com.makeramen.roundedimageview.RoundedImageView;


public class MusicPlayView extends RelativeLayout {

    public RoundedImageView musicImgView;
    public TextView musicTitleView;
    public ImageView btnPlayMode;
    public ImageView btnPlayPre;
    public ImageView btnPlayOrPause;
    public ImageView btnPlayNext;
    public ImageView btnPlayingList;
    public TextView indicatorView;

    //    public TextView nowTimeView;
//    public TextView totalTimeView;
    public SeekBar seekBar;

    public PopupWindow moreWindow;
    public View moreWindowContentView;

    public SeekBar seekBarVolume;
    public SeekBar seekBar_high_frequency;
    public SeekBar seekBarBass;

    public ImageView btnPlayMore;
    public ImageView btnTtypeSingle;
    public ImageView btnTtypeOrder;
    public ImageView btnTtypeRandom;
    public TextView textTtypeSingle;
    public TextView textTtypeOrder;
    public TextView textTtypeRandom;
    public TextView textFav;
    public ImageView btnFav;
    public TextView closeView;

    private MusicPlayView musicPlayView;

    public MusicPlayView(Context context) {
        this(context, null, 0);
    }

    public MusicPlayView(Context context,  AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MusicPlayView(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = View.inflate(context, R.layout.view_music_play, this);

        musicImgView = findViewById(R.id.playing_img);
        musicTitleView = findViewById(R.id.title);
        btnPlayMode = findViewById(R.id.play_mode);
        btnPlayOrPause = findViewById(R.id.play_or_pause);
        btnPlayPre = findViewById(R.id.play_pre);
        btnPlayNext = findViewById(R.id.play_next);
        btnPlayMore = findViewById(R.id.play_more);
//        btnPlayingList = findViewById(R.id.playing_list);
        seekBar = findViewById(R.id.seekbar);
        indicatorView = findViewById(R.id.tv_indicator);

        moreWindowContentView = View.inflate(context, R.layout.dialog_music_more, null);

        seekBarVolume = moreWindowContentView.findViewById(R.id.seekBar_volume);
        seekBarBass = moreWindowContentView.findViewById(R.id.seekBar_bass);
        seekBarBass.setMax(1000);
        seekBar_high_frequency = moreWindowContentView.findViewById(R.id.seekBar_high_frequency);
        seekBar_high_frequency.setMax(1000);

        btnTtypeSingle = moreWindowContentView.findViewById(R.id.music_type_single);
        btnTtypeOrder = moreWindowContentView.findViewById(R.id.music_type_order);
        btnTtypeRandom = moreWindowContentView.findViewById(R.id.music_type_random);
        textTtypeSingle = moreWindowContentView.findViewById(R.id.music_type_single_text);
        textTtypeOrder = moreWindowContentView.findViewById(R.id.music_type_order_text);
        textTtypeRandom = moreWindowContentView.findViewById(R.id.music_type_random_text);
        btnFav = moreWindowContentView.findViewById(R.id.fav);
        textFav = moreWindowContentView.findViewById(R.id.fav_text);

        closeView = moreWindowContentView.findViewById(R.id.close);


//        btnPlayMore.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                showMoreWindow(context);
//            }
//        });
//        closeView.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                showMoreWindow(context);
//            }
//        });
    }

    public void showMoreWindow(Context context) {

        if (moreWindow == null) {
            moreWindow = new PopupWindow(this);
            moreWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            moreWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            moreWindow.setContentView(moreWindowContentView);
            moreWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
            moreWindow.setOutsideTouchable(true);
            moreWindow.setFocusable(true);
            moreWindow.setAnimationStyle(R.style.popwindow_anim_style);
            moreWindow.setOnDismissListener(() -> {
                WindowManager.LayoutParams lp1 = ((AppCompatActivity)context).getWindow().getAttributes();
                lp1.alpha = 1f;
                ((AppCompatActivity)context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                ((AppCompatActivity)context).getWindow().setAttributes(lp1);
            });
        }
        if (moreWindow.isShowing()) {
            moreWindow.dismiss();
        } else {
            WindowManager.LayoutParams lp =((AppCompatActivity)context).getWindow().getAttributes();
            lp.alpha = 0.3f; //设置透明度
            ((AppCompatActivity)context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            ((AppCompatActivity)context).getWindow().setAttributes(lp);
            moreWindow.showAtLocation(this, Gravity.BOTTOM, 0, 0);
        }

        AudioManager am = (AudioManager)((AppCompatActivity)context).getSystemService(Context.AUDIO_SERVICE);
        //获取系统最大音量
        int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        seekBarVolume.setMax(maxVolume);

        //获取当前音量
        int currentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
        seekBarVolume.setProgress(currentVolume);
        seekBarVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(fromUser){
                    //设置系统音量
                    am.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
                    int currentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
                    seekBar.setProgress(currentVolume);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

    }


    //更新播放模式
    public void updateMusicType(int mode) {
        switch (mode){
            case MusicService.TYPE_ORDER:
                btnPlayMode.setImageResource(R.mipmap.ic_music_playrecycler);

                btnTtypeOrder.setImageResource(R.mipmap.ic_music_type_order_selected);
                btnTtypeSingle.setImageResource(R.mipmap.ic_music_type_single);
                btnTtypeRandom.setImageResource(R.mipmap.ic_music_type_random);
                textTtypeOrder.setTextColor(getResources().getColor(R.color.musicPrimary));
                textTtypeSingle.setTextColor(getResources().getColor(R.color.darkGray));
                textTtypeRandom.setTextColor(getResources().getColor(R.color.darkGray));
                break;
            case MusicService.TYPE_SINGLE:
                btnPlayMode.setImageResource(R.mipmap.ic_music_singlerecycler);

                btnTtypeOrder.setImageResource(R.mipmap.ic_music_type_order);
                btnTtypeSingle.setImageResource(R.mipmap.ic_music_type_single_selected);
                btnTtypeRandom.setImageResource(R.mipmap.ic_music_type_random);
                textTtypeOrder.setTextColor(getResources().getColor(R.color.darkGray));
                textTtypeSingle.setTextColor(getResources().getColor(R.color.musicPrimary));
                textTtypeRandom.setTextColor(getResources().getColor(R.color.darkGray));
                break;
            case MusicService.TYPE_RANDOM:
                btnPlayMode.setImageResource(R.mipmap.ic_music_random);

                btnTtypeOrder.setImageResource(R.mipmap.ic_music_type_order);
                btnTtypeSingle.setImageResource(R.mipmap.ic_music_type_single);
                btnTtypeRandom.setImageResource(R.mipmap.ic_music_type_random_selected);
                textTtypeOrder.setTextColor(getResources().getColor(R.color.darkGray));
                textTtypeSingle.setTextColor(getResources().getColor(R.color.darkGray));
                textTtypeRandom.setTextColor(getResources().getColor(R.color.musicPrimary));
                break;
            default:
        }
    }

    //更新收藏状态
    public void updateFav(boolean isFav) {
        if (isFav == true){
            btnFav.setImageResource(R.mipmap.ic_music_remove_fav_big);
            textFav.setTextColor(getResources().getColor(R.color.musicPrimary));
            textFav.setText("取消收藏");
        } else {
            btnFav.setImageResource(R.mipmap.ic_music_fav_big);
            textFav.setTextColor(getResources().getColor(R.color.darkGray));
            textFav.setText("收藏");
        }
    }

}